package com.njry.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;
import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

public class ActivitiHelper {
	
	public RepositoryService repositoryService;//获得activiti服务
	public RuntimeService runtimeService;//用于管理运行时流程实例
	public TaskService taskService;//用于管理运行时任务
	public FormService formService;//用于管理任务表单
	public HistoryService historyService;//管理流程实例、任务实例等历史数据
	public ManagementService managementService;//用于管理定时任务
	public IdentityService identityService;//用于管理组织结构
	
	public ActivitiHelper() {
		repositoryService = (RepositoryService) SpringHelper.getBean("repositoryService",RepositoryService.class);
		runtimeService = (RuntimeService) SpringHelper.getBean("runtimeService",RuntimeService.class);
		taskService = (TaskService) SpringHelper.getBean("taskService",TaskService.class);
		formService = (FormService) SpringHelper.getBean("formService",FormService.class);
		historyService = (HistoryService) SpringHelper.getBean("historyService",HistoryService.class);
		managementService = (ManagementService) SpringHelper.getBean("managementService",ManagementService.class);
		identityService = (IdentityService) SpringHelper.getBean("identityService",IdentityService.class);
	}
	
	/**
	 * activiti服务列表
	 * @return
	 */
	public List<ProcessDefinition> activitiListByprocesskey(String process_key){
		
		List<ProcessDefinition> procDefList = repositoryService.createProcessDefinitionQuery()
			.processDefinitionKey(process_key)
			.orderByProcessDefinitionVersion()
			.desc()
			.list();
		
		return procDefList;
	}
	
	/**
	 * 流程实例列表
	 * @return
	 */
	public List<ProcessInstance> listProcInstByPdfid(String pdfid){
		
		List<ProcessInstance> procInstList = runtimeService.createProcessInstanceQuery()
			.processDefinitionId(pdfid)
			.list();
		
		return procInstList;
	}
	
	/**
	 * 当前任务的执行情况
	 * @return
	 */
	public List<Execution> listExecutionByProId(String processInstanceId) {
		
		List<Execution> exectionList = runtimeService.createExecutionQuery()
			.processInstanceId(processInstanceId)
			.list();
		
		return exectionList;
	}
	
	/**
	 * 部署流程
	 * @return
	 */
	public Deployment deployProcDefByPath( String xmlPath){
		
		Deployment deploy = repositoryService.createDeployment()
        	.addClasspathResource(xmlPath)
			.deploy();
		
		return deploy;
	}
	
	/**
	 * 删除部署的流程
	 * @param process_key
	 */
	public void deleteProcDefByprocesskey(String process_key){
		List<ProcessDefinition> procDefList = this.activitiListByprocesskey(process_key);
		for(ProcessDefinition task : procDefList){
			List<Task> tasks = taskService.createTaskQuery().processDefinitionId(task.getId()).list();
			for(Task t : tasks){
				taskService.deleteTask(t.getId());
			}			
			repositoryService.deleteDeployment(task.getDeploymentId());
		}
	}
	
	

	public RepositoryService getRepositoryService() {
		return repositoryService;
	}

	public void setRepositoryService(RepositoryService repositoryService) {
		this.repositoryService = repositoryService;
	}

	public RuntimeService getRuntimeService() {
		return runtimeService;
	}

	public void setRuntimeService(RuntimeService runtimeService) {
		this.runtimeService = runtimeService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public FormService getFormService() {
		return formService;
	}

	public void setFormService(FormService formService) {
		this.formService = formService;
	}

	public HistoryService getHistoryService() {
		return historyService;
	}

	public void setHistoryService(HistoryService historyService) {
		this.historyService = historyService;
	}

	public ManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(ManagementService managementService) {
		this.managementService = managementService;
	}

	public IdentityService getIdentityService() {
		return identityService;
	}

	public void setIdentityService(IdentityService identityService) {
		this.identityService = identityService;
	}
	
}
